<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.apache.commons.lang.time.DateUtils"%>
<%@page contentType="text/html; charset=UTF-8" session="false" pageEncoding="UTF-8"%>
<%@include  file="/WEB-INF/jspf/import.jspf"%>
<%
Env env = EnvUtils.getEnv();
GeliDao geliDao = GeliUtils.getDao();
GeliOrm geliOrm = GeliUtils.getOrm();

int pageNum = env.paramInt("pageNum", 1);
int pageSize = env.paramInt("pageSize", 20);

Calendar cal = Calendar.getInstance();
Date toDate = cal.getTime();
String to = env.param("to")+" 23:59:59";
if(StringUtils.isNotBlank(env.param("to"))) {
    try {
        toDate = env.parseDate(to);
    } catch(IllegalArgumentException ex) {
        ex.printStackTrace();
    }
}
request.setAttribute("to", toDate);
Date fromDate = DateUtils.addDays(toDate, -1);
String from = env.param("from")+" 00:00:00";
if(StringUtils.isNotBlank(env.param("from"))) {
    try {
        fromDate = env.parseDate(from);
    } catch(IllegalArgumentException ex) {
        ex.printStackTrace();
    }
}
request.setAttribute("from", fromDate);

SqlBuilder sqlBuilder = new SqlBuilder();
sqlBuilder.appendSql("SELECT * FROM (");
for(int i = 0; i < 10; i++) {
    if(i != 0) {
        sqlBuilder.appendSql(" UNION ALL");
    }
    sqlBuilder.appendSql(" SELECT id,club_id,topic_id,create_date FROM ac_topic_" + i + " where create_date >= ");
    sqlBuilder.appendValue(fromDate);
    sqlBuilder.appendSql(" and create_date <= ");
    sqlBuilder.appendValue(toDate);
    //sqlBuilder.appendSql(" LIMIT " + (pageNum - 1) * pageSize + "," + pageSize);
}
sqlBuilder.appendSql(" )t ORDER BY create_date DESC");
sqlBuilder.appendSql(" LIMIT " + (pageNum - 1) * pageSize + "," + pageSize);

SqlBuilder countSqlBuilder = new SqlBuilder();
countSqlBuilder.appendSql("SELECT count(1) FROM (");
for(int i = 0; i < 10; i++) {
    if(i != 0) {
        countSqlBuilder.appendSql(" UNION ALL");
    }
    countSqlBuilder.appendSql(" SELECT id,club_id,topic_id,create_date FROM ac_topic_" + i + " where create_date >= ");
    countSqlBuilder.appendValue(fromDate);
    countSqlBuilder.appendSql(" and create_date <= ");
    countSqlBuilder.appendValue(toDate);
    //countSqlBuilder.appendSql(" LIMIT " + (pageNum - 1) * pageSize + "," + pageSize);
}
countSqlBuilder.appendSql(" )t ");


//System.out.println(sqlBuilder.getSql() + "\n" + Arrays.toString(sqlBuilder.getValues()));
List<Map<String, Object>> list = geliDao.getJdbcTemplate().queryForList(sqlBuilder.getSql(), sqlBuilder.getValues());
//System.out.println(countSqlBuilder.getSql() + "\n" + Arrays.toString(countSqlBuilder.getValues()));
int total = geliDao.count(countSqlBuilder.getSql(), countSqlBuilder.getValues());
if(total > 0 && list != null) {
    List<Topic> result = new ArrayList<Topic>();
    //id,club_id,topic_id
    for(Map<String, Object> row : list) {
        //System.out.println("row: " + row);
        try {
            long clubId = Long.valueOf(row.get("club_id").toString());
            Topic topic = geliDao.find(Topic.class, clubId, row.get("id"));
            topic.setTopicAuthor(geliDao.find(User.class, topic.getUserId()));
            result.add(topic);
        } catch(RuntimeException ex) {
            ex.printStackTrace();
        }
    }
    Pager pager = new Pager();
    pager.setResultList(result);
    pager.setTotal(total);
    pager.setPageNo(pageNum);
    pager.setPageSize(pageSize);
    request.setAttribute("pager", pager);
}
%>
<style type="text/css">
.grid .gridTbody td div{height:auto;}
</style>
<div class="pageHeader">
    <form onsubmit="return navTabSearch(this);" id="pagerForm" name="searchForm" method="post">
        <input type="hidden" name="pageNum" value="${pager.pageNo}" />
        <input type="hidden" name="pageSize" value="${pager.pageSize}" />
        <div class="searchBar">
            <table class="searchContent">
                <tbody>
                    <tr>
                        <td>
                        <label>发帖时间：</label>
                        <input type="text" id="topic_create_date_from" name="from" value="<fmt:formatDate value="${from}" pattern="yyyy-MM-dd"/>" class="textInput"> - 
                        <input type="text" id="topic_create_date_to" name="to" value="<fmt:formatDate value="${to}" pattern="yyyy-MM-dd"/>" class="textInput">
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="subBar">
                <ul>
                    <li><div class="buttonActive"><div class="buttonContent"><button type="submit">查询</button></div></div></li>
                </ul>
            </div>
        </div>
    </form>
</div>
<div class="pageContent">
    <table class="table" style="width: 100%" layoutH="110">
        <thead>
            <tr>
                <th width="5%">帖子ID</th>
                <th width="5%">版块</th>
                <th width="15%">标题</th>
                <th width="25%">描述</th>
                <th width="5%">作者</th>
                <th width="7%">发表时间</th>
                <th >图片</th>
                <th width="3%">操作</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="topic" items="${pager.resultList}">
                <tr >
                    <td>${topic.topicId}<!-- ${topic.clubId} --></td>
                    <td>${topic.forumName}</td>
                    <td><a href="http://bbs.pcauto.com.cn/topic-${topic.topicId}.html" target="_blank">${fn:escapeXml(topic.topicTitle)}</a></td>
                    <td>${fn:escapeXml(topic.message)}</td>
                    <td>${fn:escapeXml(topic.topicAuthor.nickname) }</td>
                    <td>
                        <fmt:formatDate value="${topic.createDate}" pattern="yyyy-MM-dd"/>
                    </td>
                    <td>
                        <c:if test="${!empty topic.appImage1}"><img src="${fn:escapeXml(topic.appImage1)}" width="100" />&nbsp;</c:if>
                        <c:if test="${!empty topic.appImage2}"><img src="${fn:escapeXml(topic.appImage2)}" width="100"/>&nbsp;</c:if>
                        <c:if test="${!empty topic.appImage3}"><img src="${fn:escapeXml(topic.appImage3)}" width="100"/>&nbsp;</c:if>
                    </td>
                    <td><a href="http://bbs.pcauto.com.cn/topic-${topic.topicId}.html" target="_blank">查看</a></td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</div>
<div class="panelBar">
    <div class="pages">
        <span>共 ${pager.total} 条</span>
    </div>
    <div class="pagination" targetType="navTab" totalCount="${pager.total}" numPerPage="${pager.pageSize}" pageNumShown="10" currentPage="${pager.pageNo}"></div>
</div>
<script type="text/javascript">
    //初始化日期插件；限制日期的选择范围
    $(function(){
        $("#topic_create_date_from").focus(function(){
            var val = $("#topic_create_date_to").val();
            var opts = {
                minDate : "2010-01-01"
            };
            if(val) {
                opts.maxDate = val;
            }
            $(this).datepicker(opts);
        });
        
        $("#topic_create_date_to").focus(function(){
            var val = $("#topic_create_date_from").val() || "2010-01-01";
            var opts = {};
            if(val) {
                opts.minDate = val;
            }
            $(this).datepicker(opts);
        });
    });
</script>